package com.level2app.ui.app;


import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.level2app.R;
import com.level2app.adapter.HomeCategoriesAdapter;
import com.level2app.base.BaseFragment;
import com.level2app.bean.HomeCategoriesBean;
import com.level2app.databinding.FragmentHomeBinding;
import com.level2app.ui.app.view.HomeFrgViewModel;
import com.level2app.ui.home.CategoryActivity;
import com.level2app.ui.home.HomeCateFragment;
import com.level2app.ui.home.NotificationsActivity;
import com.level2app.ui.search.SearchActivity;
import com.level2app.ui.search.SearchAddressActivity;
import com.level2app.utils.FragmentUtils;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.ToastHelper;
import com.level2app.view.RVLinearSmoothScroller;
import com.level2app.view.dialog.MyBottomSheetDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeFrgViewModel> {

    private HomeCategoriesAdapter categoriesAdapter;
    private List<HomeCategoriesBean> categoriesData;

    private final List<Fragment> mFragments = new ArrayList<>();
    private boolean isCreator = true;//是否是creator
    private RVLinearSmoothScroller linearSmoothScroller;

    @Override
    protected HomeFrgViewModel bindModel() {
        return getViewModel(this, HomeFrgViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initData() {
        showLocationPermissions();
        //通知消息数量
        binding.includeLayout.notifyNumTv.setText("8");
        categoriesData = new ArrayList<>();
        categoriesAdapter = new HomeCategoriesAdapter();
        binding.homeRV.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        binding.homeRV.setAdapter(categoriesAdapter);
        linearSmoothScroller = new RVLinearSmoothScroller(binding.homeRV.getContext());

        ArrayList<String> topData = new ArrayList<>();
        topData.add("Hotel & Resort");
        topData.add("Tourism");
        topData.add("Live Events");
        topData.add("Grand Openings/\nLaunch Product");
        for (int i = 0; i < 4; i++) {
            HomeCategoriesBean homeCategoriesBean = new HomeCategoriesBean();
            homeCategoriesBean.setName(topData.get(i));
            if (i == 0 && isCreator) {
                homeCategoriesBean.setChoose(true);
            }
            categoriesData.add(homeCategoriesBean);
        }
        categoriesAdapter.submitList(categoriesData);

        for (int i = 0; i < categoriesData.size(); i++) {
            mFragments.add(HomeCateFragment.newInstance(i));
        }
        FragmentUtils.add(getChildFragmentManager(), mFragments, R.id.frameLayout, 0);
    }

    private void showLocationPermissions() {
        XXPermissions.with(getActivity())
                .permission(Permission.ACCESS_FINE_LOCATION)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            //TODO:
                            //设置位置城市
                            binding.includeLayout.cityNameTv.setText("Miami");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        binding.includeLayout.cityNameTv.setText("locating");
                        if (never) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(getActivity(), permissions);
                        }
                    }
                });
    }

    @Override
    protected void initClick() {
        super.initClick();
        viewModel.onDelayClick(binding.includeLayout.locationLl, o -> {
            //定位
//            if (isCreator) {
//                JumpIntent.jump(getActivity(), SearchAddressActivity.class);
//            }
//            MyBottomSheetDialog dialog=new MyBottomSheetDialog(mContext);
//            dialog.show();

//            BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(mContext);
//            View view1 = getLayoutInflater().inflate(R.layout.dialog_my_bottom_sheet, null);
//            mBottomSheetDialog.setContentView(view1);
////            mBottomSheetDialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundColor(Color.TRANSPARENT);
//            mBottomSheetDialog.show();

            MyBottomSheetDialog dialog = new MyBottomSheetDialog(mContext) {
                @Override
                public void confirm(String type) {

                }
            };
            dialog.showDialog();
        });
        viewModel.onDelayClick(binding.includeLayout.searchIv, o -> {
            //搜索
            if (isCreator) {
                JumpIntent.jump(getActivity(), SearchActivity.class);
            }
        });
        viewModel.onDelayClick(binding.includeLayout.notifyIv, o -> {
            //通知
            if (isCreator) {
                JumpIntent.jump((Activity) mContext, NotificationsActivity.class);
            }
        });
        viewModel.onDelayClick(binding.categoriesAllTv, o -> {
            //全部分类
            if (isCreator) {
                JumpIntent.jump(getActivity(), CategoryActivity.class);
            }
        });

        categoriesAdapter.setOnItemClickListener((baseQuickAdapter, view, pos) -> {
            if (!isCreator) return;
            for (int i = 0; i < categoriesData.size(); i++) {
                HomeCategoriesBean categoriesBean = categoriesData.get(i);
                categoriesBean.setChoose(i == pos);
            }
            categoriesAdapter.notifyDataSetChanged();
            FragmentUtils.showHide(pos, mFragments);
            linearSmoothScroller.setTargetPosition(pos);
            new Handler().postDelayed(() -> Objects.requireNonNull(binding.homeRV.getLayoutManager()).startSmoothScroll(linearSmoothScroller), 100);
        });
    }

}